package com.vogue.meeting.common.security.exception;

import com.alibaba.fastjson.JSON;
import com.vogue.meeting.common.web.domain.Result;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 客户端身份验证
 */
@Component
public class CustomTokenEndpointAuthenticationFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (authorization==null){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            response.getWriter().write(JSON.toJSONString(Result.failure(HttpStatus.UNAUTHORIZED.value(),"客户端信息不存在")));
            return;
        }
        //TODO 登录时Client     base64那个

        //TODO 其他接口的token  登录成功下放的token

        filterChain.doFilter(request,response);
    }
}
